package com.tiantian.quanminxiu.dialog;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;

import com.jakewharton.rxbinding.view.RxView;
import com.shenglian.utils.utils.CommonUtils;
import com.tencent.rtmp.TXLiveConstants;
import com.tiantian.quanminxiu.R;
import com.tiantian.quanminxiu.api.SocketUtils;
import com.tiantian.quanminxiu.event.LiveRoomEvent;
import com.tiantian.quanminxiu.utils.APPUtils;
import com.tiantian.quanminxiu.utils.ActivityBase;
import com.tiantian.quanminxiu.utils.Constants;
import com.tiantian.quanminxiu.utils.EventBusUtils;
import com.tiantian.quanminxiu.utils.UserData;
import com.tiantian.quanminxiu.wxapi.WXEntryActivity;

import rx.functions.Action1;

public class Dialog_StartLive extends ActivityBase {

    //region 初始化数据
    private ImageView imgShareWeiXin,imgShareFriend,imgShareQQ,imgShareZone,imgShareWB;
    private View txtStartLive,vBackOut;
    private String NickName;
    private EditText etLiveContent;
    private RadioGroup rgVideoQuality;
    private int Anchor_id;
    private int shareType = -1;     //分享类型 1微信 2朋友圈 3qq好友 4空间 5微博
    private boolean isShare;
    private boolean isLand;
    //endregion


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isLand = getIntent().getBooleanExtra("isLand",false);
        if(isLand){
            if(this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            }
        }
        setContentView(R.layout.dialog_startlive);

        Init();
        InitView();
        NickName = getIntent().getStringExtra("NickName");
        Anchor_id = getIntent().getIntExtra("Anchor_id",-1);
        if(Anchor_id<0){
            CommonUtils.showToask(thisContext,"用户房间错误，请重试");
            EventBusUtils.getInstance().post(new LiveRoomEvent("", "finish"));
            finish();
        }
        shareType = 2;
        imgShareFriend.setImageResource(R.drawable.icon_share_friend_p);
    }

    //region 按钮监听 OC_Button
    private Action1<View> OC_Button = v->{
        String title = etLiveContent.getText().toString();
        switch (v.getId()) {
            case R.id.txtStartLive:
                if(TextUtils.isEmpty(title)){
                    CommonUtils.showToask(thisActivity,"开播主题不能为空，请填写..");
                    return;
                }
                //分享类型 1微信 2朋友圈 3qq好友 4空间
                switch (shareType){
                    case -1:
                        SocketUtils.getInstance().sendMessage("Join", UserData.Base.User_ID, UserData.Current.mid + "", Constants.APP_TYPE, UserData.Current.sign);
                        EventBusUtils.getInstance().post(new LiveRoomEvent(title, "load",getVideoQuality()));
                        finish();
                        break;
                    case 1:
                        isShare = true;
                        WXEntryActivity.APPShare(thisActivity, title,Anchor_id, NickName, 0);
                        break;
                    case 2:
                        isShare = true;
                        WXEntryActivity.APPShare(thisActivity,title, Anchor_id, NickName, 1);
                        break;
                    case 3:
                        isShare = true;
                        APPUtils.QQHelperLogin_To(thisActivity, 1, Anchor_id,NickName,title);
                        break;
                    case 4:
                        isShare = true;
                        APPUtils.QQHelperLogin_To(thisActivity,2,Anchor_id,NickName,title);
                        break;
                    case 5:
                        isShare = true;
                        APPUtils.SinaShare_To(thisActivity,1,Anchor_id,NickName,title);
                        break;
                }
                break;
            case R.id.imgShareWeiXin:
                imgShareWeiXin.setImageResource(R.drawable.icon_share_weixin);
                imgShareFriend.setImageResource(R.drawable.icon_share_friend);
                imgShareQQ.setImageResource(R.drawable.icon_share_qq);
                imgShareZone.setImageResource(R.drawable.icon_share_zone);
                imgShareWB.setImageResource(R.drawable.icon_share_weibo);

                if(shareType!=1){
                    shareType = 1;
                    imgShareWeiXin.setImageResource(R.drawable.icon_share_weixin_p);
                }else{
                    shareType = -1;
                }
                break;
            case R.id.imgShareFriend:
                imgShareWeiXin.setImageResource(R.drawable.icon_share_weixin);
                imgShareFriend.setImageResource(R.drawable.icon_share_friend);
                imgShareQQ.setImageResource(R.drawable.icon_share_qq);
                imgShareZone.setImageResource(R.drawable.icon_share_zone);
                imgShareWB.setImageResource(R.drawable.icon_share_weibo);

                if(shareType!=2){
                    shareType = 2;
                    imgShareFriend.setImageResource(R.drawable.icon_share_friend_p);
                }else{
                    shareType = -1;
                }
                break;
            case R.id.imgShareQQ:
                imgShareWeiXin.setImageResource(R.drawable.icon_share_weixin);
                imgShareFriend.setImageResource(R.drawable.icon_share_friend);
                imgShareQQ.setImageResource(R.drawable.icon_share_qq);
                imgShareZone.setImageResource(R.drawable.icon_share_zone);
                imgShareWB.setImageResource(R.drawable.icon_share_weibo);

                if(shareType!=3){
                    shareType = 3;
                    imgShareQQ.setImageResource(R.drawable.icon_share_qq_p);
                }else{
                    shareType = -1;
                }
                break;
            case R.id.imgShareZone:
                imgShareWeiXin.setImageResource(R.drawable.icon_share_weixin);
                imgShareFriend.setImageResource(R.drawable.icon_share_friend);
                imgShareQQ.setImageResource(R.drawable.icon_share_qq);
                imgShareZone.setImageResource(R.drawable.icon_share_zone);
                imgShareWB.setImageResource(R.drawable.icon_share_weibo);

                if(shareType!=4){
                    shareType = 4;
                    imgShareZone.setImageResource(R.drawable.icon_share_zone_p);
                }else{
                    shareType = -1;
                }
                break;
            case R.id.imgShareWB:
                imgShareWeiXin.setImageResource(R.drawable.icon_share_weixin);
                imgShareFriend.setImageResource(R.drawable.icon_share_friend);
                imgShareQQ.setImageResource(R.drawable.icon_share_qq);
                imgShareZone.setImageResource(R.drawable.icon_share_zone);
                imgShareWB.setImageResource(R.drawable.icon_share_weibo);

                if(shareType!=5){
                    shareType = 5;
                    imgShareWB.setImageResource(R.drawable.icon_share_weibo_p);
                }else{
                    shareType = -1;
                }
                break;
            case R.id.vBackOut:
                EventBusUtils.getInstance().post(new LiveRoomEvent("", "finish"));
                hideKeyboard(thisActivity);
                finish();
                break;
            default:
                break;
        }
    };
    //endregion

    //region 隐藏软键盘 hideKeyboard
    public void hideKeyboard(Context context) {
        Activity activity = (Activity) context;
        if (activity != null) {
            InputMethodManager imm = (InputMethodManager) activity
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm.isActive() && activity.getCurrentFocus() != null) {
                imm.hideSoftInputFromWindow(activity.getCurrentFocus()
                        .getWindowToken(), 0);
            }
        }
    }
    //endregion

    @Override
    public void InitView() {
        txtStartLive = findViewById(R.id.txtStartLive);
        vBackOut = findViewById(R.id.vBackOut);
        etLiveContent = (EditText) findViewById(R.id.etLiveContent);
        imgShareWeiXin = (ImageView) findViewById(R.id.imgShareWeiXin);
        imgShareFriend = (ImageView) findViewById(R.id.imgShareFriend);
        imgShareQQ = (ImageView) findViewById(R.id.imgShareQQ);
        imgShareZone = (ImageView) findViewById(R.id.imgShareZone);
        imgShareWB = (ImageView) findViewById(R.id.imgShareWB);

        rgVideoQuality = (RadioGroup) findViewById(R.id.rgVideoQuality);

        RxView.clicks(vBackOut).map(View -> vBackOut).subscribe(OC_Button);
        RxView.clicks(txtStartLive).map(View -> txtStartLive).subscribe(OC_Button);
        RxView.clicks(imgShareWeiXin).map(View->imgShareWeiXin).subscribe(OC_Button);
        RxView.clicks(imgShareFriend).map(View->imgShareFriend).subscribe(OC_Button);
        RxView.clicks(imgShareQQ).map(View->imgShareQQ).subscribe(OC_Button);
        RxView.clicks(imgShareZone).map(View->imgShareZone).subscribe(OC_Button);
        RxView.clicks(imgShareWB).map(View->imgShareWB).subscribe(OC_Button);
    }

    @Override
    public void PageInit() {
        if(isShare){
            SocketUtils.getInstance().sendMessage("Join", UserData.Base.User_ID, UserData.Current.mid + "", Constants.APP_TYPE, UserData.Current.sign);
            EventBusUtils.getInstance().post(new LiveRoomEvent(etLiveContent.getText().toString(), "load",getVideoQuality()));
            finish();
        }
    }

    //region 按回退键退出 onKeyDown(int keyCode, KeyEvent event)
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            EventBusUtils.getInstance().post(new LiveRoomEvent("", "finish"));
            hideKeyboard(thisActivity);
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    //endregion

    public int getVideoQuality(){
        switch (rgVideoQuality.getCheckedRadioButtonId()){
            case R.id.rbQualiteSuper:
                return TXLiveConstants.VIDEO_QUALITY_SUPER_DEFINITION;
            case R.id.rbQualiteHigh:
                return TXLiveConstants.VIDEO_QUALITY_HIGH_DEFINITION;
            case R.id.rbQualiteStandard:
                return TXLiveConstants.VIDEO_QUALITY_STANDARD_DEFINITION;
        }
        return TXLiveConstants.VIDEO_QUALITY_STANDARD_DEFINITION;
    }


    @Override
    protected void onResume() {
        super.onResume();
        PageInit();
    }

}